
from Agent.Astar.Astar_Home import *
from Config import *


def AStarRewardBlockValue(env,horizon, vertical, player):

    if player == 1:
        nowstate = env.play1state
        opponent = 2
    if player == 2:
        nowstate = env.play2state
        opponent = 1

    rewardNewState = {}

    for reward_point in env.reward_states:
        if (reward_point // Config.CHECKERBOARDSIZE in horizon) and (reward_point % Config.CHECKERBOARDSIZE in vertical):

            aStarSelf = AStar(startpoint=nowstate, endpoint=reward_point, env=env, player=player)
            actionSelf, lengthSelf = aStarSelf.action()

            aStarOpp = AStar(startpoint=nowstate, endpoint=reward_point, env=env, player=opponent)
            actionOpp, lengthOpp = aStarOpp.action()

            rewardNewState[reward_point] = env.reward_states[reward_point]*(lengthSelf/(lengthSelf + lengthOpp))

    lenReward = 0
    rewardCount = 0

    while len(rewardNewState) > 0:
        lshort = None
        for reward_point in rewardNewState:
            if lshort == None:
                aStar = AStar(startpoint=nowstate, endpoint=reward_point, env=env, player=player)
                actionNow, lengthNow = aStar.action()
                reward_point_temp = reward_point
            else:
                aStar = AStar(startpoint=nowstate, endpoint=reward_point, env=env, player=player)
                actionTemp, lengthTemp = aStar.action()
                if lengthTemp < lengthNow:
                    reward_point_temp = reward_point
                    lengthNow = lengthTemp
                    actionNow = actionTemp
        lenReward = lengthNow + lenReward
        rewardCount = rewardCount + rewardNewState[reward_point_temp]
        nowstate = reward_point_temp
        rewardNewState.pop(reward_point_temp)

    return lenReward, rewardCount



def AStarRewardBlock(env, horizon, vertical, player):

    if player == 1:
        nowstate = env.play1state
        opponent = 2
    if player == 2:
        nowstate = env.play2state
        opponent = 1



    rewardNewState = {}

    for reward_point in env.reward_states:
        if (reward_point // Config.CHECKERBOARDSIZE in horizon) and (reward_point % Config.CHECKERBOARDSIZE in vertical):

            aStarSelf = AStar(startpoint=nowstate, endpoint=reward_point, env=env, player=player)
            actionSelf, lengthSelf = aStarSelf.action()

            aStarOpp = AStar(startpoint=nowstate, endpoint=reward_point, env=env, player=opponent)
            actionOpp, lengthOpp = aStarOpp.action()

            rewardNewState[reward_point] = env.reward_states[reward_point]*(lengthSelf/(lengthSelf + lengthOpp))

    lenReward = 0
    rewardCount = 0

    lshort = None

    for reward_point in rewardNewState:
        if lshort == None:
            aStar = AStar(startpoint=nowstate, endpoint=reward_point, env=env, player=player)
            actionNow, lengthNow = aStar.action()
            reward_point_temp = rewardNewState[reward_point]/lengthNow
        else:
            aStar = AStar(startpoint=nowstate, endpoint=reward_point, env=env, player=player)
            actionTemp, lengthTemp = aStar.action()
            if reward_point_temp < rewardNewState[reward_point]/lengthTemp:
                reward_point_temp = rewardNewState[reward_point]/lengthTemp
                lengthNow = lengthTemp
                actionNow = actionTemp

    return actionNow










